Many programming languages, such as C, C++ and Java, allow a function to take a variable number of input arguments, but these languages only allow the function to return a single value. Some languages, such as MATLAB®, allow functions to return more than one value. For example, a function provided in MATLAB® programming environments may return both the eigenvalues and eigenvectors of a matrix, or an approximation function may return both the coefficients of the approximation and an estimate of the accuracy of the approximation. This feature is valuable in mathematical functions as well as more general situations.
In conventional MATLAB® programming environments, when a function is called, the function may query to ask how many input arguments and output arguments it was called with. The function may return as many output arguments as defined, but if the caller of the function has supplied fewer output arguments than the function can return, the excess output arguments are ignored by the function, and the memory space for the excess output arguments is reclaimed. This works if the desired output arguments come at the beginning of the function's output argument list. At times, however, some output arguments that come at the beginning of the function's output argument list are not desired and other output arguments that come later are desired.
As an example, MATLAB® programming environments provide a sort function, as described below. The sort function can return two output arguments including a sorted vector and a permutation used to sort an input vector. At times, only the permutation is desired. In conventional MATLAB®-based programming environments, a temporary array (Temp) is created to hold the sorted vector:[Temp,I]=sort(V);
An easy programming error in using a temporary array is to forget to clear the temporary array, which means that the memory space taken by the temporary arrays may be wasted. The programming environment, or some programs that analyze other programs looking for errors, may check whether a temporary array is assigned to and not used. While this helps the programmer remember to clear the temporary array, clearing the temporary array may draw another message that the value assigned to the temporary array has never been used. Furthermore, the sort program may perform extra operations to produce values for the temporary array, which will not be used.